/*
 * DG (DG Gaming Engine)  - API
 * Copyright (C) 2004  Bob Marks (marksie531@yahoo.com)
 * http://code.google.com/p/dng-gaming
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 */
package com.dg.gaming.client;

import com.dg.gaming.api.common.*;
import com.dg.gaming.api.common.msg.*;

/**
 * <p>Convience class for sending message from a table client frame in DG.</p>
 *
 * <p>This class also has links to several methods in the ClientConnectionThread
 * class through delegation.</p>
 *
 * <p>This class holds a link to the ClientConnectionThread (as there should
 * only be one instance per client) and a tableNum which it appends to messages
 * automatically when a send method is called.</p>
 *
 * @author Bob Marks
 * @version Alpha 0.2.3
 */
public class TableConnectionThread {

    /**
     * Link to the server.
     */
    private TableConnection conn;

    /**
     * Table number.
     */
    private String tableNum;

    /**
     * Constructor which takes a ClientConnectionThread and a table number.
     *
     * @param conn      Connection to server.
     * @param tableNum  Current table number.
     */
    public TableConnectionThread(TableConnection conn, String tableNum) {
        this.conn = conn;
        this.tableNum = tableNum;
    }

    /**
     * @param message
     */
    public void send(MsgTableMessage message) {
        message.setTableNum(this.tableNum);

        this.conn.send(message);
    }

    /**
     * Return the table number.
     *
     * @return  Table number
     */
    public String getTableNum() {
        return this.tableNum;
    }

    /**
     * Return the table associated with this table connection class.
     *
     * @param message
     */
    public Table getTable() {

        if (conn instanceof TableConnection) {
            return ((TableConnection) conn).getTable();
        }

        return null;
    }

    /**
     * Return the game object on this connection.
     *
     * @return
     */
    public Game getGame() {
        return conn.getTable().getGame();
    }

    // Delegate methods from ClientConnectionThread

    /**
     * Send a game message.
     *
     * @param message   Game message.
     */
    public void send(MsgGameMessage message) {
        this.conn.send(message);
    }

    /**
     * Delegate method for returning a username;
     *
     * @return   Username.
     */
    public long getUsername() {
        return this.conn.getUserId();
    }

    /**
     * Delegate method for returning the table list.
     *
     * @return  List of tables.
     */
    public TableList getTableList() {

        return getGame().getTableList();
    }

    /**
     * Delegate method for returning the user list.
     *
     * @return  List of users.
     */
    public UserList getUserList() {
        return getGame().getUserList();
    }

    /**
     * Return thue if this user is the owner of the table.
     *
     * @return
     */
    public boolean isOwnerOfTable() {
        return this.conn.getUserId() ==
                getTable().getOwner();
    }

    /**
     * Return the client connection thread which is a field.
     *
     * @return  Current connection to server.
     */
    public AbstractConnectionThread getClientConnectionThread() {
        return this.conn;
    }

    /**
     * getUserid
     *
     * @return long
     */
    public long getUserid() {
        return this.conn.getUserId();
    }
}
